package com.sca.order.controller;

import com.alibaba.csp.sentinel.slots.block.RuleConstant;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
import com.sca.order.entity.api.BaseResult;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liuxianfei
 * @since 2020/11/3 18:53
 */
@Api
@RestController
public class TestController {

    @GetMapping("/test")
    public BaseResult<?> test() {
        //规则集合
        List<FlowRule> rules = new ArrayList<>();
        //创建新的规则对象,"/list"就是资源名
        FlowRule rule = new FlowRule("/list");
        //"直接"策略
        rule.setStrategy(RuleConstant.STRATEGY_DIRECT);
        //采用QPS限速模式
        rule.setGrade(RuleConstant.FLOW_GRADE_QPS);
        //限制为QPS 为100
        rule.setCount(100);
        //流控效果设置为"预热"
        rule.setControlBehavior(RuleConstant.CONTROL_BEHAVIOR_WARM_UP);
        //预热时间20秒
        rule.setWarmUpPeriodSec(20);
        //来源设置为默认所有default
        rule.setLimitApp("default");
        rules.add(rule);
        //设置,FlowRuleManager是Sentinel规则管理器
        FlowRuleManager.loadRules(rules);
        return new BaseResult<>();
    }

}
